package com.metagurukul.metaspace.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.metagurukul.metaspace.models.beans.Project;
import com.metagurukul.metaspace.util.Constants;

/**
 * @author ritesh gupta
 * this class is for managing project
 *
 */
public class ProjectDao extends DatabaseDAO {
	/** to return list of project
	 * @return list
	 * @throws SQLException
	 */
	public List<Project> getRecordList ()
	throws SQLException {
		ArrayList<Project> projectList = null;
		String query = "Select * from Project";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			rs = ps.executeQuery();
			projectList = new ArrayList<Project>();
			while (rs.next()) {
				Project project = new Project();
				project.setProjectId(rs.getInt("project_id"));
				project.setProjectName(rs.getString("project_name"));
				projectList.add(project);
			}
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		return projectList;
		
	}
	
	/**to get a project name of a project with given id.
	 * @param projectId
	 * @return name of project
	 * @throws SQLException
	 */
	public String getName(int projectId) throws SQLException{
		String query = "Select project_name from Project where project_id = ?";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			ps.setInt(1, projectId);
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getString("project_name");
			}
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR  + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		return null;
	}
}
